import api from '../../utils/api';

Page({

  data: {
    newsList: []
  },

  onLoad: function (options) {
    this.getNewsList();
  },

  getNewsList() {
    const _this = this;
    wx.showLoading({
      title: '请稍后...'
    });
    api({
      url: '/User/queryNewsList',
      method: 'GET',
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          newsList: res.data.data
        });
      }
    });
  },

  // 下拉刷新
  onPullDownRefresh() {
    var that = this;
    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    });
  },

  toLike(e) {
    const { id } = e.currentTarget.dataset;
    const _this = this;
    wx.showLoading({
      title: '请稍后...'
    });
    api({
      url: '/User/likeNews',
      method: 'POST',
      data: {
        id
      },
      success: function (res) {
        wx.hideLoading();
        _this.getNewsList();
      }
    });
  },

  toCollect(e) {
    const { id } = e.currentTarget.dataset;
    const _this = this;
    wx.showLoading({
      title: '请稍后...'
    });
    api({
      url: '/User/collectNews',
      method: 'POST',
      data: {
        id
      },
      success: function (res) {
        wx.hideLoading();
        _this.getNewsList();
      }
    });
  }
})